package com.itdr.common;

import com.fasterxml.jackson.annotation.JsonIgnore;
import com.fasterxml.jackson.annotation.JsonInclude;
import lombok.Data;

import java.io.Serializable;

/**
 * ClassName: ServerResponse
 * 日期: 2020/10/21 15:01
 *
 * @author Air张
 * @since JDK 1.8
 */
@Data
@JsonInclude(JsonInclude.Include.NON_NULL)
public class ServerResponse<T> implements Serializable {
    private Integer code;
    private T data;
    private String msg;

    // 成功方法使用的构造函数
    private ServerResponse(Integer code, String msg, T data) {
        this.code = code;
        this.msg = msg;
        this.data = data;
    }


    public static <T> ServerResponse toSuccess(T data) {
        return new ServerResponse(Const.SUCCESS,null,data);
    }

    public static <T> ServerResponse toFail(Integer errorCode, String errorMessage) {
        return new ServerResponse(errorCode, errorMessage,null);
    }
    public static <T> ServerResponse toFail(String errorMessage) {
        return new ServerResponse(Const.FAIL,errorMessage,null);
    }
    public static <T> ServerResponse toFail(Integer errorCode, String errorMessage, T data) {
        return new ServerResponse(errorCode, errorMessage, data);
    }

    //让success不在json序列化结果之中
    @JsonIgnore
    public boolean isSuccess() {
        return this.code.equals(Const.SUCCESS);
    }
}
